<?php

namespace App\Task\Recommend;

use App\Amqp\Producer\UpdateUserScoreProducer;
use App\Cache\RedisKey;
use App\Cache\UserScoreCache;
use App\Constants\RecommendedGradeCode;
use App\Helpers\RedisUtil;
use App\Model\UserProfile;
use App\Model\UserScore;
use App\Service\ServiceTrait\UpdateScoreTrait;
use Hyperf\Amqp\Producer;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Utils\ApplicationContext;

/**
 * 每天定时修改用户评级
 * @Crontab(name="RecommendedScoreTask", rule="0 5 * * *", callback="execute", memo="每天定时修改用户评级", singleton=false)
 */
class RecommendedScoreTask
{
    use UpdateScoreTrait;

    public function execute()
    {
        $time = date("Y-m-d", strtotime('-'.RecommendedGradeCode::ACTIVE_DAY.' day'));

        $userIds = UserProfile::getRecentlyActiveUserIdByTime($time.' 00:00:00');

        UserScoreCache::delAllScore();

        foreach ($userIds as $one)
        {
            $message = new UpdateUserScoreProducer(['user_id' => $one]);
            $producer = ApplicationContext::getContainer()->get(Producer::class);
            $producer->produce($message);
        }

        $this->resetFixUserData();
    }
}